package com.zhangqiancheng.wcdmaconsole;

import java.net.DatagramPacket;

public class WcdmaRequest {

	private String type = "00";
	private String protocol = "00";
	private String content = "";
	
	public WcdmaRequest(String type, String protocol) {
		this.type = type;
		this.protocol = protocol;
	}
	
	public WcdmaRequest(String type, String protocol, String content) {

		this.type = type;
		this.protocol = protocol;
		this.content = content;
	}



	public String getType() {

		return type;
	}
	
	public void setType(String type) {

		this.type = type;
	}

	public String getProtocol() {

		return protocol;
	}
	
	public void setProtocol(String protocol) {

		this.protocol = protocol;
	}

	public String getTypeAndProtocol() {

		return type + protocol;
	}
	
	public String getContent() {

		return content;
	}
	
	public void setContent(String content) {

		this.content = content;
	}
	
	@Override
	public String toString() {
	
		return "0000";
	}
	
	public static WcdmaRequest createTurnOnRequest() {
		return new WcdmaRequest("00", "01");
	}
	
	public static WcdmaRequest createTurnOffRequest() {
		return new WcdmaRequest("00", "04");
	}
	
	public static WcdmaRequest createUpdateSettingsRequest(String content) {
		return new UpdateSettingsRequest(content);
	}
	
	public static WcdmaRequest createUpdate2GRequest(String content) {
		return new Update2GRequest(content);
	}

	public static WcdmaRequest parse(DatagramPacket packet) {

		String requestMessage = new String(packet.getData(), packet.getOffset(), packet.getLength());

		String typeAndProtocol = requestMessage.substring(0, 4);
		String content = "";
		if (requestMessage.length() > 4)
			content = requestMessage.substring(4);

		WcdmaRequest result = null;
		
		switch (typeAndProtocol) {
		case "0001":
			result =  createTurnOnRequest();
			break;
		case "0004":
			result = createTurnOffRequest();
			break;
		case "0301":
			result = createUpdateSettingsRequest(content);//updateSettingsAndRespond(udpServer);
			break;
		case "0614":
			result = createUpdate2GRequest(content);
			break;
		}

		return result;
	}

}
